package com.ruoyi.web.publish.resume.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.publish.resume.mapper.ResumeMapper;
import com.ruoyi.web.publish.resume.domain.Resume;
import com.ruoyi.web.publish.resume.service.IResumeService;
import org.springframework.util.CollectionUtils;

/**
 * 我的简历Service业务层处理
 *
 * @author liuyu
 * @date 2021-08-24
 */
@Service
public class ResumeServiceImpl implements IResumeService {
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询我自己的简历
     *
     * @return 我的简历
     */
    @Override
    public Resume getMyResume() {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();

        Resume resume = new Resume();
        resume.setCreateById(user.getUserId());

        List<Resume> resumeList = resumeMapper.selectResumeList(resume);
        if (CollectionUtils.isEmpty(resumeList)) {
            return resume;
        } else {
            return resumeList.get(0);
        }
    }

    /**
     * 根据用户ID查询所属人的简历
     *
     * @return 简历
     */
    @Override
    public Resume getResumeToBelongs(Long userId) {
        Resume resume = new Resume();
        resume.setCreateById(userId);

        List<Resume> resumeList = resumeMapper.selectResumeList(resume);
        if (CollectionUtils.isEmpty(resumeList)) {
            return resume;
        } else {
            return resumeList.get(0);
        }
    }

    /**
     * 查询我的简历
     *
     * @param id 我的简历主键
     * @return 我的简历
     */
    @Override
    public Resume selectResumeById(Long id) {
        return resumeMapper.selectResumeById(id);
    }

    /**
     * 查询我的简历列表
     *
     * @param resume 我的简历
     * @return 我的简历
     */
    @Override
    public List<Resume> selectResumeList(Resume resume) {
        return resumeMapper.selectResumeList(resume);
    }

    /**
     * 新增我的简历
     *
     * @param resume 我的简历
     * @return 结果
     */
    @Override
    public int insertResume(Resume resume) {
        resume.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resume.setCreateById(user.getUserId());
        resume.setCreateBy(user.getUserName());
        resume = this.calculateScore(resume);
        return resumeMapper.insertResume(resume);
    }

    /**
     * 修改我的简历
     *
     * @param resume 我的简历
     * @return 结果
     */
    @Override
    public int updateResume(Resume resume) {
        resume.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resume.setUpdateById(user.getUserId());
        resume.setUpdateBy(user.getUserName());
        resume = this.calculateScore(resume);
        return resumeMapper.updateResume(resume);
    }

    /**
     * 批量删除我的简历
     *
     * @param ids 需要删除的我的简历主键
     * @return 结果
     */
    @Override
    public int deleteResumeByIds(Long[] ids) {
        return resumeMapper.deleteResumeByIds(ids);
    }

    /**
     * 删除我的简历信息
     *
     * @param id 我的简历主键
     * @return 结果
     */
    @Override
    public int deleteResumeById(Long id) {
        return resumeMapper.deleteResumeById(id);
    }

    /**
     * 计算得分
     *
     * @param resume 我的简历
     * @return 我的简历
     */
    @Override
    public Resume calculateScore(Resume resume) {
        BigDecimal scoreSum = BigDecimal.ZERO;

        // 管理能力
        if (null != resume.getClassPosition()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getClassPosition()));
        }
        if (null != resume.getSchoolPosition()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getSchoolPosition()));
        }
        if (null != resume.getOutsideSchoolPosition()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getOutsideSchoolPosition()));
        }

        // 学习能力
        if (null != resume.getEnglishAbility()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getEnglishAbility()));
        }
        if (null != resume.getScholarship()) {
            if ("0".equals(resume.getScholarship())) {
                scoreSum = scoreSum.add(new BigDecimal(1));
            } else {
                scoreSum = scoreSum.add(new BigDecimal(resume.getEnglishAbility()));
            }
        }

        // 写作能力
        if (null != resume.getPaper()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getPaper()));
        }
        if (null != resume.getcPaper()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getcPaper()));
        }
        if (null != resume.getCorePaper()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getCorePaper()));
        }
        if (null != resume.getSubject()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getSubject()));
        }

        // 动手能力
        if (null != resume.getPatent()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getPatent()));
        }
        if (null != resume.getWinner()) {
            if (null != resume.getWinnerCount()) {
                BigDecimal winScore = new BigDecimal(resume.getWinner()).multiply(new BigDecimal(resume.getWinnerCount()));
                scoreSum = scoreSum.add(winScore);
            } else {
                scoreSum = scoreSum.add(new BigDecimal(resume.getWinner()));
            }
        }

        // 股票安全性
        if (null != resume.getEndorsement()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getEndorsement()));
        }
        if (null != resume.getMargin()) {
            scoreSum = scoreSum.add(new BigDecimal(resume.getMargin()));
        }

        resume.setScore(scoreSum.doubleValue());
        return resume;
    }
}
